package messageACL;
import jade.lang.acl.ACLCodec;
import jade.lang.acl.ACLMessage;
import java.io.*; 

/**
 * 
 */

/**
 * @author Jim Mainprice, Konstantin Thierbach
 *
 */
public class MessageConversion implements ACLCodec {

	/* (non-Javadoc)
	 * @see jade.lang.acl.ACLCodec#decode(byte[], java.lang.String)
	 */
	@Override
	public ACLMessage decode(byte[] arg0, String arg1) throws CodecException {
		ByteArrayInputStream bais 	= new ByteArrayInputStream(arg0);
		ObjectInputStream ois;
		ACLMessage acl = null; 
		try {
			ois = new ObjectInputStream(bais);
			acl = (ACLMessage)ois.readObject();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
		return acl;
	}

	/* (non-Javadoc)
	 * @see jade.lang.acl.ACLCodec#encode(jade.lang.acl.ACLMessage, java.lang.String)
	 */
	@Override
	public byte[] encode(ACLMessage arg0, String arg1) {
		ByteArrayOutputStream baos 	= new ByteArrayOutputStream();
		ObjectOutputStream oos;
		byte[] acl	= null;
		try {
			oos 		= new ObjectOutputStream(baos);
			oos.writeObject(arg0);
			oos.close();
			acl = baos.toByteArray();
			baos.close();
		}catch(IOException e) {
			e.printStackTrace();
		}
		return acl;
	}

	/* (non-Javadoc)
	 * @see jade.lang.acl.ACLCodec#getName()
	 */
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

}
